package kr.co.webmill;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ContactPickerTester extends Activity {

	private static final int PICK_CONTACT = 1;
	private static final int ANO_MENU = Menu.FIRST;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contactpickertester);
		Button button = (Button) findViewById(R.id.pick_contact_button);
		
		button.setOnClickListener( new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
				//Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/"));
				Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://com.android.contacts/contacts"));
				startActivityForResult(intent, PICK_CONTACT);
				
			}
		});
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode){
		case (PICK_CONTACT) :
			Uri contactData = data.getData();
			Cursor c = managedQuery(contactData, null, null, null, null);
			c.moveToFirst();
			String name;
			name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
			TextView tv = (TextView) findViewById(R.id.selected_contact_textview);
			tv.setText(name);

		break;
		}
	}	
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		Intent intent = new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT);
				
		//MenuItem itemAdd = menu.add(0, ANO_MENU, Menu.NONE, R.string.ano_menu);
		Intent[] specifics = null;
		ComponentName caller = getComponentName();
		int flags = Menu.FLAG_APPEND_TO_GROUP;
		MenuItem[] outSpecificItems = null;
		menu.addIntentOptions(0, ANO_MENU, Menu.NONE, caller, specifics, intent, flags, outSpecificItems);
		return true;

		
	}
    /*
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT);
		
		return true;
	}    
	*/

}
